package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.TaxRateEntity;
import cn.com.huzo.repository.master.TaxRateRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.master.TaxRateManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 税率管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("TaxRateManager")
public class TaxRateManagerImpl extends GenericManagerImpl<TaxRateEntity, Long>
        implements TaxRateManager {

    @Resource(name = "TaxRateRepository")
    private TaxRateRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<TaxRateEntity> fetchAll() throws Exception {
        return _dao.fetchAll();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public TaxRateEntity fetchCurrentTaxRate() throws Exception {
        return _dao.fetchCurrentTaxRate();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveLines(Map<String, ? extends List<TaxRateEntity>> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;
        if (criterion.get("insertList").size() != 0) {
            _rowsAffected1 = _dao.batchInsert(criterion.get("insertList"));
        }
        if (criterion.get("updateList").size() != 0) {
            _rowsAffected2 = _dao.batchUpdate(criterion.get("updateList"));
        }

        if (_rowsAffected1 > 0) {
            // 审计日志
            writeAuditLog(criterion.get("insertList").get(0).getUpdatedBy(),
                    "基础信息→税率", "税率：" + criterion.get("insertList").get(0).getTaxRateText(),
                    "增加");
        }
        if (_rowsAffected2 > 0) {
            // 审计日志
            writeAuditLog(criterion.get("updateList").get(0).getUpdatedBy(),
                    "基础信息→税率", "税率：" + criterion.get("updateList").get(0).getTaxRateText(),
                    "更新");
        }

        if ((_rowsAffected1 + _rowsAffected2) > 0) {
            rtnValue.put("LinesList", _dao.fetchAll());
        }
        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(TaxRateEntity instance) throws Exception {
        Long key = _dao.insert(instance);
        // 审计日志
        if (key > 0) {
            writeAuditLog(instance.getUpdatedBy(), "基础信息→税率", "税率：" + instance.getTaxRateText(), "增加");
        }
        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "TaxRateId"));
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "基础信息→税率", "税率：" + MapUtils.getString(criterion, "TaxRateText"),
                    "删除");
        }

        rtnValue.put("LinesList", _dao.fetchAll());
        return rtnValue;
    }
}
